﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ClassLib.Classes.Common;
using DevExpress.XtraEditors;
using DevExpress.XtraSplashScreen;
using ISumen;
using Sumen.Business;
using Sumen.Business.Model;
using Sumen.Business.Model.Presenter.Actor;
using Sumen.Business.Model.Presenter.Category;
using Sumen.Business.Model.Presenter.Sys;
using Sumen_Stock.LookAndFeel.Sumen_System;
using Sumen.Business.Model.Presenter;

namespace Sumen_Stock.LookAndFeel.Entities
{
    public partial class Frm_Customer : DevExpress.XtraEditors.XtraForm
    {
        Frm_Customer_Edit frm_customer_edit;
        private FromCode _fromCode = FromCode.FrmCustomer;
        private CustomerPresenter _customerPresenter;
        private CustomerTypePresenter _customerTypePresenter;

        public Frm_Customer()
        {
            InitializeComponent();
            _customerPresenter = new CustomerPresenter();
            _customerTypePresenter = new CustomerTypePresenter();
            //if (ClsPublic.Writelog == "1")
            //{
            //    SysLogPresenter log = new SysLogPresenter();
            //    log.Add(_fromCode, EnumFormStatus.View, "", "");
            //}
        }

        private void Frm_Customer_Load(object sender, EventArgs e)
        {
            LoadForm();
        }

        private void LoadForm()
        {
            gridControl1.DataSource = _customerPresenter.GetAll();
            repositoryItemLookUpEdit1.DataSource = _customerTypePresenter.GetAll();
        }

        public override void Refresh()
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            base.Refresh();
            LoadForm();
            SplashScreenManager.CloseForm();
        }

        #region Button Event

        private void buttonsArray1_btnEventAdd_click(object sender, EventArgs e)
        {
            if (frm_customer_edit == null)
            {
                frm_customer_edit = new Frm_Customer_Edit();
                frm_customer_edit.EnumFormStatus = EnumFormStatus.Add;
                frm_customer_edit.ShowDialog();
            }
            else
            {
                frm_customer_edit.EnumFormStatus = EnumFormStatus.Add;
                frm_customer_edit.ShowDialog();
            }
        }

        private void buttonsArray1_btnEventClose_click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonsArray1_btnEventDelete_click(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                if (ClsMsgBox.XacNhanXoaThongTin())
                {
                    if (_customerPresenter.Delete(sUsername) > 0)
                    {
                        ClsMsgBox.ThongTin("Xóa khách hàng thành công");
                       
                        ClsPublic.WriteLog(_fromCode, EnumFormStatus.Add, sUsername, "");
                        Refresh();
                    }
                }
            }
        }

        private void buttonsArray1_btnEventUpdate_click(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                customer cus = _customerPresenter.GetById(sUsername);
                if (cus != null)
                {
                    if (frm_customer_edit == null)
                    {
                        frm_customer_edit = new Frm_Customer_Edit();
                        frm_customer_edit.Customer = cus;
                        frm_customer_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_customer_edit.ShowDialog();
                    }
                    else
                    {
                        frm_customer_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_customer_edit.Customer = cus;
                        frm_customer_edit.ShowDialog();
                    }
                }
                else
                {
                    ClsMsgBox.Loi("Không tìm thấy khách hàng.");
                }
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            string sUsername = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, colUsername) + string.Empty;
            {
                customer cus = _customerPresenter.GetById(sUsername);
                if (cus != null)
                {
                    if (frm_customer_edit == null)
                    {
                        frm_customer_edit = new Frm_Customer_Edit();
                        frm_customer_edit.Customer = cus;
                        frm_customer_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_customer_edit.ShowDialog();
                    }
                    else
                    {
                        frm_customer_edit.EnumFormStatus = EnumFormStatus.Modify;
                        frm_customer_edit.Customer = cus;
                        frm_customer_edit.ShowDialog();
                    }
                }
                else
                {
                    ClsMsgBox.Loi("Không tìm thấy khách hàng.");
                }
            }
        }

        #endregion

        







    }
}